<?php 

require_once ('Zend/Controller/Plugin/Abstract.php');

/**
 * 
 */
class App_Controller_Plugin_ViewSetup extends Zend_Controller_Plugin_Abstract
{
	/**
	 * Set value to title of browser
	 * @see Zend_Controller_Plugin_Abstract::postDispatch()
	 */
	public function postDispatch(Zend_Controller_Request_Abstract $request) 
	{
		// if not the request has been dispatched
		if (!$request->isDispatched()) {
			return;
		}
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
		$view = $bootstrap->getResource('view');
		if ( $view->title ) {
			$view->headTitle($view->title);
			$view->headTitle()->setSeparator(' - ');
		}
	}
	
	/** 
	 * switch navigation
	 * @see Zend_Controller_Plugin_Abstract::routeShutdown()
	 */
	public function routeShutdown(Zend_Controller_Request_Abstract $request) {
		// config the view of zend_navigation
		$this->configViewNavigation($request);
	}
	
	/**
	 * 
	 * @param Zend_Controller_Request_Abstract $request
	 */
	private function configViewNavigation(Zend_Controller_Request_Abstract $request) {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
		
    	$navigation = Zend_Registry::get('navigation');
    	$view = $bootstrap->getResource('view');
    	$layout = $bootstrap->getResource('layout');
		
		// load sub menu
		$menuPrincipal = $navigation->findBy('id', $request->getModuleName());
        if ($menuPrincipal) {
        	// menu principal equal (1ualifications, educations,  occupations) or (user managemente, role management)
        	$menuPrincipal->setActive(true);
        	$layout->menuPrincipal = $menuPrincipal;
        }
	}
}